package com.cxrj.zhsq.interfaceManage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxrj.zhsq.propertyManage.dao.NoticeUserMapper;
import com.cxrj.zhsq.propertyManage.service.NoticeSecvice;
import com.cxrj.zhsq.utils.IpUtil;

@Controller
@RequestMapping(value = "/interface")
public class InterfaceNoticeController {

	@Resource
	private NoticeSecvice noticeSecvice;

	@Resource
	private NoticeUserMapper noticeUserMapper;

	/**
	 * 查询未读消息条数
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectNoticeByCount", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectNoticeByCount(String userId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count", noticeUserMapper.selectByCount(userId));
		return map;
	}

	/**
	 * 查询最新3条消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectNoticeByTopThree", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectNoticeByTopThree(String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = noticeSecvice.selectNoticeByTopThree(userId);
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/zhsq_xg/");
		return resultMap;
	}

	/**
	 * 根据状态查询消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectNoticeByStatus", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectNoticeByStatus(String userId, String status, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = noticeSecvice.selectNoticeByStatus(userId, status, dataIndex, pageCount);
		resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/zhsq_xg/");
		return resultMap;
	}

	/**
	 * 根据id查询消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectNoticeId", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectNoticeId(String id, String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = noticeSecvice.selectById(id, userId, request);
		return resultMap;
	}

	/**
	 * 修改已读
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateNoticeByRead", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> updateNoticeByRead(String userId, String noticeId, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = noticeSecvice.updateNoticeByRead(userId, noticeId);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}
	
	/**
	 * 根据未读消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectNoticeByNotRead", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectNoticeByNotRead(String userId, HttpServletRequest request) {
		Map<String, Object> resultMap = noticeSecvice.selectNoticeByNotRead(userId);
		return resultMap;
	}
}
